package org.geoscheduler.android.maps;

import java.util.List;

import org.geoscheduler.android.R;
import org.geoscheduler.model.Event;
import org.geoscheduler.model.Event.EventType;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class MapOverviewOverlay extends Overlay {
	
	public List<Event> events;

	public GeoPoint currentLocation;
	
	protected Bitmap eventPrivateSymbol; 

	protected Bitmap eventPublicSymbol; 

	protected Bitmap pointSymbol; 

	public MapOverviewOverlay(Context context) {
		super();
		eventPrivateSymbol = BitmapFactory.decodeResource(context.getResources(), R.drawable.map_symbol_event);
		eventPublicSymbol = BitmapFactory.decodeResource(context.getResources(), R.drawable.map_symbol_public_event);
		pointSymbol = BitmapFactory.decodeResource(context.getResources(), R.drawable.map_symbol_point);
	}

	/**
	 * @see com.google.android.maps.Overlay#draw(android.graphics.Canvas, com.google.android.maps.MapView, boolean)
	 */
	@Override
	public void draw(Canvas canvas, MapView mapView,
			boolean shadow) {
		super.draw(canvas, mapView, shadow);
		
		if (events != null) {
			for (Event event: events) {
				if (event.getLocation() != null) {
					if (event.getEventTypeEnum() == EventType.PRIVATE) {
						drawSymbol(canvas, mapView.getProjection(), event.getTitle(), eventPrivateSymbol, event.getLocation().getLat(), event.getLocation().getLon(), -1 * eventPrivateSymbol.getWidth() / 2, -1 * eventPrivateSymbol.getHeight() / 2);
					} else {
						drawSymbol(canvas, mapView.getProjection(), event.getTitle(), eventPublicSymbol, event.getLocation().getLat(), event.getLocation().getLon(), -1 * eventPublicSymbol.getWidth() / 2, -1 * eventPublicSymbol.getHeight() / 2);
					}
				}
			}
		}
		
		if (currentLocation != null) {
			drawSymbol(canvas, mapView.getProjection(), null, pointSymbol, currentLocation.getLatitudeE6(), currentLocation.getLongitudeE6(), -1 * pointSymbol.getWidth() / 2, -1 * pointSymbol.getHeight());
		}
	}

	protected void drawSymbol(Canvas canvas, Projection projection, String desc, Bitmap symbol, int lat, int lon, int xshift, int yshift) {
		
		Point screenPoint = new Point();
		GeoPoint eventLoc = new GeoPoint(lat, lon);
		projection.toPixels(eventLoc, screenPoint);
		
		int xy[] = new int[]{screenPoint.x, screenPoint.y};
		
		Paint paint = new Paint();
		paint.setStyle(Style.FILL);
		paint.setARGB(255, 0, 0, 0);
		canvas.drawBitmap(symbol, xy[0] + xshift, xy[1] + yshift, paint);
		
		if (desc != null) {
			paint.setARGB(50, 0, 0, 0);
			float tmpSize = paint.measureText(desc);
			canvas.drawRect(xy[0] - tmpSize / 2 - 3, xy[1] - 8, xy[0] + tmpSize / 2 + 3, xy[1]+ 8, paint);			
			paint.setARGB(255, 255, 255, 255);
			canvas.drawText(desc, xy[0] - tmpSize / 2, xy[1] + 5, paint);
		}
	}
}
